package com.yixin.vuehr.web.controller.system.basic;

import com.yixin.vuehr.bean.RespBean;
import com.yixin.vuehr.model.Department;
import com.yixin.vuehr.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;
    @GetMapping("/")
    public List<Department> getAllDepartments(){
        return departmentService.getAllDepartments();
    }
    @PostMapping("/")
    public RespBean addDep(@RequestBody Department dep){
        departmentService.addDep(dep);
        if (dep.getResult() == 1){
            return RespBean.ok("添加成功！",dep);
        }
        return RespBean.error("添加失败");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteDep(@PathVariable Integer id){
        Department dep = new Department();
        dep.setId(id);
        departmentService.deleteDep(dep);
        if (dep.getResult()==-2){
            return RespBean.error("有子部门，删除失败！");
        } else if (dep.getResult()==-1){
            return RespBean.error("该部们下有员工，删除失败！");
        } else if (dep.getResult()==1){
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败");
    }
}
